#include "VideoOcrCommon.hpp"
#include "VideoOcr.hpp"


int main(){
	//初始化
	CVideo* p_cv=new CVideo();	std::vector<cv::Rect> f_rects,c_rects;
	cv::Mat rec_image,find_image,draw_image;
	double max_time=p_cv->m_frameNumbers/p_cv->m_fps;
//	double start_time=180.0;
//	double end_time=240.0;
//	cv::Rect rec(350,600,600,70);
	double start_time=0;
	double end_time=max_time;
	cv::Rect rec(500,900,920,70);

	//打开视频文件
	p_cv->OpenMovie("/home/knife/test.mp4",0);
	//输出视频基本参数
	printf("width:%ld height:%ld fps:%f frameNumbers:%f\n",p_cv->m_Width,p_cv->m_Height,p_cv->m_fps,p_cv->m_frameNumbers);
	//输出视频首帧
	//cv::imwrite("/home/knife/test.jpg",p_cv->m_cur_frame);

	if (start_time>max_time) { start_time=0.0; }
	if (end_time>max_time) { end_time=max_time; }
	//开始逐秒遍历视频
	for (double i=1.0;i<14;i++){
	//double i=186.00;
		//转换秒时间
		int hh,mm,ss,tmp;
		hh=(int)i / 3600;
		tmp=((int)i) % 3600;
		mm=tmp / 60;
		ss=tmp % 60;
		//跳转视频到指定秒帧
		p_cv->SetPos(i*1000);
		//获取指定矩形图像
		if (p_cv->GetImage(rec)) {
			//将指定矩形图复制到待处理rec_image
			rec_image=p_cv->m_rec_image.clone();
//			cv::imwrite(std::string("/home/knife/test_")+std::to_string((int)i)+std::string(".jpg"),rec_image);
			//对rec_image标记轮廓find_image并获取矩形数据到f_rects
			find_image=findContoursImage(rec_image,&f_rects,0.9,true);
			printf("[%2d:%2d:%2d] f_rects.size=%ld",hh,mm,ss,f_rects.size());
			//筛选矩形数据（特定算法）
			c_rects=filterRects(f_rects,2);
			if (c_rects.size()>0){
				//在源图画出矩形框
				//以下，可以输出图像，也可以改成OCR识别操作
				draw_image=drawRect(rec_image,c_rects);
				printf("[%2d:%2d:%2d] c_rects.size=%ld",hh,mm,ss,c_rects.size());
				cv::imwrite(std::string("/home/knife/test_")+std::to_string((int)i)+std::string(".jpg"),draw_image);
			}
		}

	}
	p_cv->CloseMovie();
	
	return 0;

}
